﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CLDataAppManagement;
using DataFileApp_WPF.Models;
using DataFileApp_WPF.ViewModels;
using Microsoft.Win32;
using Newtonsoft.Json.Linq;

namespace DataFileApp_WPF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DataFileApp : Window
    {
        public DataFileApp()
        {
            InitializeComponent();
         
            
            this.myDataGrid.DataContext = dataGridItem.DataGridCols;

            this.typeCombox.DataContext = dataGridItem.DropDowmList;
            this.keyText.DataContext = dataGridItem_Data;
            this.valueText.DataContext = dataGridItem_Data;
            this.typeCombox.DataContext = dataGridItem_Data;

           this.DataContext = new DataFileAppModel();
        }
        //************************************* 变量 *****************************************
        private bool isDragging = false;
        private Point startPoint;

        // 用于存储窗口的原始位置、大小和状态
        private double originalLeft;
        private double originalTop;
        private double originalWidth;
        private double originalHeight;
        private WindowState originalWindowState;

        DataAppManagement dataAppManagement = new DataAppManagement();
        JToken jToken;


        DataGridItem dataGridItem = new DataGridItem();
        DataGridItem dataGridItem_Data = new DataGridItem();



        private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }




        private void Label_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                if (WindowState == WindowState.Normal)
                {
                    WindowState = WindowState.Maximized;
                }
                else if (WindowState == WindowState.Maximized)
                {
                    WindowState = WindowState.Normal;
                }
            }
        }

        private void btn_file_Click(object sender, RoutedEventArgs e)
        {
            if (ExpandedButtonsPanel_file.Visibility == Visibility.Collapsed)
            {
                // 如果隐藏，则设置为可见
                ExpandedButtonsPanel_file.Visibility = Visibility.Visible;
                btn_fileimage.Width = 20;
                btn_fileimage.Height = 20;
                btn_file.Width = 40;
                btn_file.Height = 35;
            }
            else
            {
                // 如果可见，则设置为隐藏
                ExpandedButtonsPanel_file.Visibility = Visibility.Collapsed;
                btn_fileimage.Width = 30;
                btn_fileimage.Height = 30;
                btn_file.Width = 50;
                btn_file.Height = 45;
            }


        }

        private void btn_language_Click(object sender, RoutedEventArgs e)
        {
            if (ExpandedButtonsPanel_languge.Visibility == Visibility.Collapsed)
            {
                // 如果隐藏，则设置为可见
                ExpandedButtonsPanel_languge.Visibility = Visibility.Visible;
                btn_language_image.Width = 20;
                btn_language_image.Height = 20;
                btn_language.Width = 40;
                btn_language.Height = 35;
            }
            else
            {
                // 如果可见，则设置为隐藏
                ExpandedButtonsPanel_languge.Visibility = Visibility.Collapsed;
                btn_language_image.Width = 30;
                btn_language_image.Height = 30;
                btn_language.Width = 50;
                btn_language.Height = 45;
            }
        }

        private void btn_about_Click(object sender, RoutedEventArgs e)
        {

        }





        private void minBox_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void maxBox_Click(object sender, RoutedEventArgs e)
        {


            if (this.WindowState == WindowState.Maximized)
            {
                // 恢复窗口到原始的位置、大小和状态
                this.Left = originalLeft;
                this.Top = originalTop;
                this.Width = originalWidth;
                this.Height = originalHeight;
                this.WindowState = WindowState.Normal;



                //this.WindowState = WindowState.Normal;
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = new Uri("/Resources/Image/Max.png", UriKind.Relative);
                bitmap.EndInit();
                Image image = this.maxBox.Content as Image;
                if (image != null)
                {
                    // 将 BitmapImage 对象赋值给 Image 控件的 Source 属性
                    image.Source = bitmap;
                }

            }
            else
            {
                // 记录窗口的原始状态
                originalLeft = this.Left;
                originalTop = this.Top;
                originalWidth = this.Width;
                originalHeight = this.Height;
                originalWindowState = WindowState.Maximized;

                // 获取不包含任务栏的工作区域
                Rect workArea = SystemParameters.WorkArea;
                // 设置窗口的位置和大小以实现最大化且不遮挡任务栏
                this.Left = workArea.Left;
                this.Top = workArea.Top;
                this.Width = workArea.Width;
                this.Height = workArea.Height;
                this.WindowState = WindowState.Maximized;

                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = new Uri("/Resources/Image/Normal.png", UriKind.Relative);
                bitmap.EndInit();
                Image image = this.maxBox.Content as Image;
                if (image != null)
                {
                    // 将 BitmapImage 对象赋值给 Image 控件的 Source 属性
                    image.Source = bitmap;
                }
            }
        }

        private void close_Click(object sender, RoutedEventArgs e)
        {

            this.Close();
        }


     

        private void AddJTokenToTreeView(JToken jToken, ref TreeItem treeViewItem, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "NoError";
            try
            {
                switch (jToken.Type)
                {
                    case JTokenType.None:
                        break;
                    case JTokenType.Object:
                        foreach (JProperty property in jToken.Children<JProperty>())
                        {
                            if (property.Value.Type != JTokenType.Array && property.Value.Type != JTokenType.Object)
                            {
                                continue;
                            }
                            TreeItem childItem = new TreeItem();
                            childItem.Name = property.Name;
                            childItem.ImagePath = "/Resources/Image/class.png";
                            childItem.Parent = treeViewItem;
                            treeViewItem.Children.Add(childItem);

                            AddJTokenToTreeView(property.Value, ref childItem, out ErrorCode, out ErrorText);
                        }
                        break;
                    case JTokenType.Array:
                        treeViewItem.ImagePath = "/Resources/Image/array.png";
                        int Length = jToken.Count();
                        for (int i = 0; i < jToken.Count(); i++)
                        {
                            //数组中的类
                            if (jToken[i].Type == JTokenType.Object)
                            {
                                //遍历数组下的类元素
                                foreach (JProperty property in jToken[i].Children<JProperty>())
                                {
                                    TreeItem childItem = new TreeItem();
                                    childItem.Name = property.Name;
                                    childItem.ImagePath = "/Resources/Image/array.png";
                                    childItem.Parent = treeViewItem;
                                    childItem.IsArray = true;
                                    childItem.ArrayIndex = i;
                                    treeViewItem.Children.Add(childItem);
                                    AddJTokenToTreeView(property.Value, ref childItem, out ErrorCode, out ErrorText);
                                }
                            }
                            else
                            {
                                AddJTokenToTreeView(jToken[i], ref treeViewItem, out ErrorCode, out ErrorText);
                            }
                        }
                        break;
                    case JTokenType.Constructor:
                        break;
                    case JTokenType.Property:
                        break;
                    case JTokenType.Comment:
                        break;
                    case JTokenType.Integer:



                        break;
                    case JTokenType.Float:
                        break;
                    case JTokenType.String:
                        break;
                    case JTokenType.Boolean:
                        break;
                    case JTokenType.Null:
                        break;
                    case JTokenType.Undefined:
                        break;
                    case JTokenType.Date:
                        break;
                    case JTokenType.Raw:
                        break;
                    case JTokenType.Bytes:
                        break;
                    case JTokenType.Guid:
                        break;
                    case JTokenType.Uri:
                        break;
                    case JTokenType.TimeSpan:
                        break;
                    default:
                        break;
                }

            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = "AddJTokenToTreeView Fail！" + ex.Message;
            }
        }
        private void btn_openFile_Click(object sender, RoutedEventArgs e)
        {
            //OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.Filter = "所有文件|*.*|cdat文件|*.cdat ";

            //if (openFileDialog.ShowDialog() == true)
            //{
            //    jToken = dataAppManagement.LoadFile(openFileDialog.FileName, out int ErrorCode, out string ErrorText);
            //    if (jToken != null)
            //    {
            //        TreeItem rootItem = new TreeItem();
            //        AddJTokenToTreeView(jToken, ref rootItem, out ErrorCode, out ErrorText);
            //    }
            //}

            JToken jToken = dataAppManagement.GetToken("root.arry1.[0:array11]", out _, out _);
            // dataAppManagement.CopyToAfterDes("root.arry1.[0:array11]","", out _, out _);

        }
        private void btn_newFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "所有文件|*.*|cdat文件|*.cdat ";

            //if (openFileDialog.ShowDialog() == true)
            //{
            jToken = dataAppManagement.LoadFile(@"D:\New_Plug_In\Item9_DataApp_Development\3.0版本\WindowsFormsApp14\WindowsFormsApp14\file.json", out int ErrorCode, out string ErrorText);
            if (jToken != null)
            {
                TreeItem rootItem = new TreeItem();

                AddJTokenToTreeView(jToken, ref rootItem, out ErrorCode, out ErrorText);
                this.treeView.ItemsSource = rootItem.Children;

            }
            //}
        }

        private void btn_saveFile_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_saveFileAs_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_chineseLaguage_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_englishLanguage_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_germanLanguage_Click(object sender, RoutedEventArgs e)
        {

        }

        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var selectedNode = e.NewValue as TreeItem;
            dataGridItem.DataGridCols.Clear();
            if (selectedNode != null)
            {
                string path = GetNodePath(selectedNode);
                var jt = dataAppManagement.GetToken(path, out int ErrorCode, out string ErrorText);
                DataGridItem dGI;
                //节点       
                if (jt.Type == JTokenType.Property)
                {
                 
                    //数组节点，元素为值
                    if (((JProperty)jt).Value.Type == JTokenType.Array)
                    {
                        int index = 0;
                        foreach (var item in jt.Children().Children())
                        {
                            switch (item.Type)
                            {
                                case JTokenType.None: break;
                                case JTokenType.Object: break;

                                case JTokenType.Array: break;
                                case JTokenType.Constructor: break;
                                case JTokenType.Property: break;
                                case JTokenType.Comment: break;
                                case JTokenType.Integer: 
                                    dGI = new DataGridItem();
                                    if (item.Parent.Type == JTokenType.Array)
                                    {
                                        dGI.Key = $"[{index}]";
                                        dGI.Value = item;
                                        dGI.Type = item.Type.ToString();
                                        dataGridItem.DataGridCols.Add(dGI);
                                        index += 1;
                                    }
                                    break;
                                case JTokenType.Float:
                                    dGI = new DataGridItem();
                                    if (item.Parent.Type == JTokenType.Array)
                                    {
                                        dGI.Key = $"[{index}]";
                                        dGI.Value = item;
                                        dGI.Type = item.Type.ToString();
                                        dataGridItem.DataGridCols.Add(dGI);
                                        index += 1;
                                    }
                                    break;
                                case JTokenType.String:
                                    dGI = new DataGridItem();
                                    if (item.Parent.Type == JTokenType.Array)
                                    {
                                        dGI.Key = $"[{index}]";
                                        dGI.Value = item;
                                        dGI.Type = item.Type.ToString();
                                        dataGridItem.DataGridCols.Add(dGI);
                                        index += 1;
                                    }
                                    break;
                                case JTokenType.Boolean: break;
                                case JTokenType.Null: break;
                                case JTokenType.Undefined: break;
                                case JTokenType.Date: break;
                                case JTokenType.Raw: break;
                                case JTokenType.Bytes: break;
                                case JTokenType.Guid: break;
                                case JTokenType.Uri: break;
                                case JTokenType.TimeSpan: break;
                                default:
                                    break;
                            }
                        }
                    }
                    else if (((JProperty)jt).Value.Type == JTokenType.Object)
                    {

                        //类 键值对 类/数组/值
                        foreach (var item in jt.Children().Children().Children())
                        {
                            switch (item.Type)
                            {
                                case JTokenType.None: break;
                                case JTokenType.Object: break;
                                case JTokenType.Array: break;
                                case JTokenType.Constructor: break;
                                case JTokenType.Property: break;
                                case JTokenType.Comment: break;
                                case JTokenType.Integer: 
                                    dGI = new DataGridItem();
                                    if (item.Type != JTokenType.Array && item.Type != JTokenType.Object)
                                    {
                                        dGI.Key = ((JProperty)item.Parent).Name;
                                        dGI.Value = ((JProperty)item.Parent).Value;
                                        dGI.Type = ((JProperty)item.Parent).Value.Type.ToString();
                                        dataGridItem.DataGridCols.Add(dGI);
                                    }
                                    break;
                                case JTokenType.Float:
                                    dGI = new DataGridItem();
                                    if (item.Type != JTokenType.Array && item.Type != JTokenType.Object)
                                    {
                                        dGI.Key = ((JProperty)item.Parent).Name;
                                        dGI.Value = ((JProperty)item.Parent).Value;
                                        dGI.Type = ((JProperty)item.Parent).Value.Type.ToString();
                                        dataGridItem.DataGridCols.Add(dGI);
                                    }
                                    break;
                                case JTokenType.String:
                                    dGI = new DataGridItem();
                                    if (item.Type != JTokenType.Array && item.Type != JTokenType.Object)
                                    {
                                        dGI.Key = ((JProperty)item.Parent).Name;
                                        dGI.Value = ((JProperty)item.Parent).Value;
                                        dGI.Type = ((JProperty)item.Parent).Value.Type.ToString();
                                        dataGridItem.DataGridCols.Add(dGI);
                                    }
                                    break;
                                case JTokenType.Boolean: break;
                                default:
                                    break;
                            }
                        }
                    }
                }

                //数组中的类元素
                else if (jt.Type == JTokenType.Object)
                {
                    int index = 0;
                    foreach (var item in jt.Children().Children().Children())
                    {
                        switch (item.Type)
                        {
                            case JTokenType.None: break;
                            case JTokenType.Object: break;
                            case JTokenType.Array: break;
                            case JTokenType.Constructor: break;
                            case JTokenType.Property: break;
                            case JTokenType.Comment: break;
                            case JTokenType.Integer:
                                dGI = new DataGridItem();
                                if (item.Parent.Type == JTokenType.Array)
                                {
                                    dGI.Key = $"[{index}]";
                                    dGI.Value = item;
                                    dGI.Type = item.Type.ToString();
                                    dataGridItem.DataGridCols.Add(dGI);
                                    index += 1;
                                }
                                break;
                            case JTokenType.Float:
                                dGI = new DataGridItem();
                                if (item.Parent.Type == JTokenType.Array)
                                {
                                    dGI.Key = $"[{index}]";
                                    dGI.Value = item;
                                    dGI.Type = item.Type.ToString();
                                    dataGridItem.DataGridCols.Add(dGI);
                                    index += 1;
                                }
                                break;
                            case JTokenType.String:
                                dGI = new DataGridItem();
                                if (item.Parent.Type == JTokenType.Array)
                                {
                                    dGI.Key = $"[{index}]";
                                    dGI.Value = item;
                                    dGI.Type = item.Type.ToString();
                                    dataGridItem.DataGridCols.Add(dGI);
                                    index += 1;
                                }
                                break;
                            case JTokenType.Boolean: break;
                            case JTokenType.Null: break;
                            default:
                                break;
                        }
                    }
                }
            }
        }




        //public void ShowValueToDataGrid(JToken jt, int index)
        //{
        //    switch (jt.Type)
        //    {
        //        case JTokenType.None:
        //            break;
        //        case JTokenType.Object:
        //            break;
        //        case JTokenType.Array:
        //            foreach (var item in jt.Children())
        //            {
        //                ShowValueToDataGrid(item, index);
        //            }

        //            JArray jArray = new JArray();
        //            foreach (JObject item in jArray)
        //            {

        //            }

        //            break;
        //        case JTokenType.Constructor:
        //            break;
        //        case JTokenType.Property:

        //            foreach (var item in jt.Children())
        //            {
        //                ShowValueToDataGrid(item, index);
        //            }
        //            break;
        //        case JTokenType.Comment:
        //            break;
        //        case JTokenType.Integer:
        //            index += 1;


        //            break;
        //        case JTokenType.Float:
        //            break;
        //        case JTokenType.String:
        //            break;
        //        case JTokenType.Boolean:
        //            break;
        //        case JTokenType.Null:
        //            break;
        //        case JTokenType.Undefined:
        //            break;
        //        case JTokenType.Date:
        //            break;
        //        case JTokenType.Raw:
        //            break;
        //        case JTokenType.Bytes:
        //            break;
        //        case JTokenType.Guid:
        //            break;
        //        case JTokenType.Uri:
        //            break;
        //        case JTokenType.TimeSpan:
        //            break;
        //        default:
        //            break;
        //    }
        //    if (jt.Type == JTokenType.Property)
        //    {

        //    }
        //    //数组中的元素是类
        //    else if (jt.Type == JTokenType.Object)
        //    {

        //    }
        //}









        private string GetNodePath(TreeItem node)
        {
            var pathSegments = new List<string>();
            while (node != null && node.Name != null)
            {
                string name = string.Empty;
                if (node.IsArray)
                {
                    name = $"[{node.ArrayIndex}:{node.Name}]";
                }
                else
                {
                    name = node.Name;
                }
                pathSegments.Insert(0, name);
                node = node.Parent;
            }
            return string.Join(".", pathSegments);
        }

        private void myDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.myDataGrid.SelectedItem is DataGridItem dataGridItem)
            {
                dataGridItem_Data.Key = dataGridItem.Key;
                dataGridItem_Data.Value = dataGridItem.Value;
                dataGridItem_Data.Type = dataGridItem.Type;
                
            } 
        }
    }
}
